package com.lht.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lht.admin.entity.SysUserEntity;
import com.lht.admin.utils.PageUtils;
import com.lht.admin.utils.ShiroUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Controller公共组件
 */
public abstract class AbstractController {
    protected Logger logger = LoggerFactory.getLogger(getClass());

    protected SysUserEntity getUser() {
        SysUserEntity entity = ShiroUtils.getUserEntity();
        if (entity == null) {
            entity = new SysUserEntity();
            entity.setUserId(-1L);
            entity.setUsername("Faker");
        }

        return entity;
    }

    protected Long getUserId() {
        return getUser().getUserId();
    }

    protected Long getDeptId() {
        return getUser().getDeptId();
    }

    /**
     * 转换成pageUtils返回前端
     *
     * @param page page
     * @return PageUtils
     */
    protected PageUtils parseToPageUtils(Page page) {

        return new PageUtils(
                page.getRecords(),
                (int) page.getTotal(),
                (int) page.getSize(),
                (int) page.getCurrent()
        );
    }
}
